package com.kaishengit.web.filter;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.kaishengit.entity.User;

public class LoginFilter extends AbstractFilter{

	@Override
	public void doFilter(ServletRequest req, ServletResponse resp,
			FilterChain chain) throws IOException, ServletException {
			HttpServletRequest request = (HttpServletRequest) req;
			HttpServletResponse response = (HttpServletResponse) resp;
			
			String uri = request.getRequestURI();
			
			//System.out.println(uri);
			
			String str = uri.substring(uri.lastIndexOf("/") + 1);
			
			String floder = uri.substring(uri.indexOf("/"), uri.lastIndexOf("/"));
			
			if("".equals(str) || "index.jsp".equals(str) || "index.jspx".equals(str) ||"login.jspx".equals(str)|| floder.contains("static") || uri.endsWith("jpg")) {
				chain.doFilter(request, response);
			} else {
				HttpSession session = request.getSession();
				User user = (User) session.getAttribute("user");
				
				if(user == null) {
					System.out.println("当前请求网站： " + uri + "被拒绝！" );
					response.sendRedirect("index.jspx?code=10002");
				} else {
					chain.doFilter(request, response);
					
				}
				
			}
	}

}
